
const express = require("express");
const route = express.Router();
const path = require("path")
const advModel = require("../db/model/advModel")

const isLoginMiddle = require("../middleware/isLoginMiddle");

//
const formidable = require("formidable")

//更新广告 接口
route.put("/updateAdv", isLoginMiddle, async (req, res) => {

  const { _id } = req.query
  // console.log(req.query,res);
  // console.log(_id);
  const form = formidable({
    uploadDir: path.resolve(__dirname, "../upload"),
    keepExtensions: true
  })
   form.parse(req,async (err, params, file) => {

    // //解构
    const {
      advTitle,
      advType,
      advOrder
    } = params

    try {

    await advModel.updateOne({ _id }, {
        $set: {
          advTitle,
          advType, 
          advOrder,
          advHref: "/upload/" + file.advPic.newFilename,
          updateTime: new Date().toLocaleString()
        }
      })

      // console.log(re);
      return res.send({
        code: 10000,
        message: "更新广告成功",
        data: null
      })
    } catch (e) {
      console.log("更新广告失败" + e.message);
      res.send({
        code: 10001,
        message: "更新广告失败",
        data: null
      })
    }




  })


})


module.exports = route;